package com.quartet.modules.vehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quartet.api.PageResult;
import com.quartet.modules.equipment.service.TEquipmentInfoService;
import com.quartet.modules.vehicle.dao.TVehicleInfoDao;
import com.quartet.modules.vehicle.dto.TVehicleInfoRequestDto;
import com.quartet.modules.vehicle.dto.TVehicleInfoResponseDto;
import com.quartet.modules.vehicle.dto.TVehicleInfoTotalRequestDto;
import com.quartet.modules.vehicle.dto.TVehicleInfoTotalResponseDto;
import com.quartet.modules.vehicle.entity.TVehicleInfo;
import com.quartet.modules.vehicle.service.*;
import com.quartet.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 车辆摘要信息表(TVehicleInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-05-04 17:26:14
 */
@Service("tVehicleInfoService")
public class TVehicleInfoServiceImpl extends ServiceImpl<TVehicleInfoDao, TVehicleInfo> implements TVehicleInfoService {

    @Autowired
    private TVehicleInfoDao tVehicleInfoDao;

    @Autowired
    private TVehicleOwnerInfoService tVehicleOwnerInfoService;
    @Autowired
    private TVehicleDetailInfoService tVehicleDetailInfoService;
    @Autowired
    private TTerminalEquipmentInformationService tTerminalEquipmentInformationService;
    @Autowired
    private TRelocationInfoService tRelocationInfoService;
    @Autowired
    private TPlatformEquipmentInformationService tPlatformEquipmentInformationService;
    @Autowired
    private TOilConsumptionEquipmentService tOilConsumptionEquipmentService;
    @Autowired
    private TDriverInfoService tDriverInfoService;
    @Autowired
    private TEquipmentInfoService tEquipmentInfoService;
    /**
     * 查询所有车辆数据
     * @param tVehicleInfoRequestDto
     * @return
     */
    @Override
    public PageResult<TVehicleInfoResponseDto> selectVehicleByCondition(TVehicleInfoRequestDto tVehicleInfoRequestDto) {
        PageHelper.startPage(tVehicleInfoRequestDto.getCurrent(), tVehicleInfoRequestDto.getLimit());
        PageInfo pageInfo = new PageInfo(tVehicleInfoDao.selectVehicleByCondition(tVehicleInfoRequestDto));
        Long total = pageInfo.getTotal();
        List<TVehicleInfoResponseDto> list = pageInfo.getList();
        PageResult<TVehicleInfoResponseDto> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setData(list);
        return pageResult;
    }

    /**
     * 新增车辆相关信息
     * @param tVehicleInfoTotalRequestDto
     */
    @Override
    public void addVehicleInfo(TVehicleInfoTotalRequestDto tVehicleInfoTotalRequestDto,List<MultipartFile> driverPhotoList) {
        List<String> photoImageUrlList=new ArrayList<>();
        //上传驾驶员图片
        driverPhotoList.forEach(file -> {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //得到上传时的文件名
            String filename=file.getOriginalFilename();
            //保存到项目指定目录
            String uploadDir="/opt/photo";
            //如果目录不存在，自动创建文件夹
            File dir=new File(uploadDir);
            if(!dir.exists()){
                dir.mkdir();
            }
            //保存文件对象 加上uuid是为了防止文件重名
            File serverFile=new File(uploadDir+"\\"+uuid+filename);
            try {
                file.transferTo(serverFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            photoImageUrlList.add(serverFile.getAbsolutePath());
        });
        tVehicleInfoTotalRequestDto.getTDriverInfoAddRequestDto().setDriverPhoto(photoImageUrlList);
        if (!ObjectUtils.isEmpty(tVehicleInfoTotalRequestDto.getTVehicleInfoAddRequestDto())){
            TVehicleInfo tVehicleInfo = BeanUtil.copyProperties(tVehicleInfoTotalRequestDto, TVehicleInfo.class);
            tVehicleInfo.setVehicleCode(RandomUtils.getVehicleCode());
            tVehicleInfoDao.insert(tVehicleInfo);
        }
        tVehicleOwnerInfoService.addVehicleOwnerInfo(tVehicleInfoTotalRequestDto.getTVehicleOwnerInfoAddRequestDto());
        tVehicleDetailInfoService.addVehicleDetailInfo(tVehicleInfoTotalRequestDto.getTVehicleDetailInfoAddRequestDto());
        tTerminalEquipmentInformationService.addTerminalEquipmentInformation(tVehicleInfoTotalRequestDto.getTTerminalEquipmentInformationAddRequestDto());
        tRelocationInfoService.addRelocationInfo(tVehicleInfoTotalRequestDto.getTRelocationInfoAddRequestDto());
        tPlatformEquipmentInformationService.addPlatformEquipmentInformation(tVehicleInfoTotalRequestDto.getTPlatformEquipmentInformationAddRequestDto());
        tOilConsumptionEquipmentService.addOilConsumptionEquipment(tVehicleInfoTotalRequestDto.getTOilConsumptionEquipmentAddRequestDto());
        tDriverInfoService.addDriverInfoInfo(tVehicleInfoTotalRequestDto.getTDriverInfoAddRequestDto());
        tEquipmentInfoService.addEquipmentInfo(tVehicleInfoTotalRequestDto.getTEquipmentInfoAddRequestDto());
    }

    /**
     * 功能描述: 修改车辆相关信息
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/5/13 16:07
     * @param tVehicleInfoTotalRequestDto
     * @return void
     */
    @Override
    public void updateVehicleInfo(TVehicleInfoTotalRequestDto tVehicleInfoTotalRequestDto) {
        if (!ObjectUtils.isEmpty(tVehicleInfoTotalRequestDto.getTVehicleInfoAddRequestDto())){
            TVehicleInfo tVehicleInfo = BeanUtil.copyProperties(tVehicleInfoTotalRequestDto, TVehicleInfo.class);
            tVehicleInfoDao.updateById(tVehicleInfo);
        }
        tVehicleOwnerInfoService.updateVehicleOwnerInfo(tVehicleInfoTotalRequestDto.getTVehicleOwnerInfoAddRequestDto());
        tVehicleDetailInfoService.updateVehicleDetailInfo(tVehicleInfoTotalRequestDto.getTVehicleDetailInfoAddRequestDto());
        tTerminalEquipmentInformationService.updateTerminalEquipmentInformation(tVehicleInfoTotalRequestDto.getTTerminalEquipmentInformationAddRequestDto());
        tRelocationInfoService.updateRelocationInfo(tVehicleInfoTotalRequestDto.getTRelocationInfoAddRequestDto());
        tPlatformEquipmentInformationService.updatePlatformEquipmentInformation(tVehicleInfoTotalRequestDto.getTPlatformEquipmentInformationAddRequestDto());
        tOilConsumptionEquipmentService.updateOilConsumptionEquipment(tVehicleInfoTotalRequestDto.getTOilConsumptionEquipmentAddRequestDto());
        tDriverInfoService.updateDriverInfoInfo(tVehicleInfoTotalRequestDto.getTDriverInfoAddRequestDto());
        tEquipmentInfoService.updateEquipmentInfo(tVehicleInfoTotalRequestDto.getTEquipmentInfoAddRequestDto());
    }

    /**
     * deleteById
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        TVehicleInfo tVehicleInfo = tVehicleInfoDao.selectById(id);
        tVehicleInfoDao.deleteById(id);
        tVehicleOwnerInfoService.deleteVehicleOwnerInfo(tVehicleInfo.getVehicleCode());
        tVehicleDetailInfoService.deleteVehicleDetailInfo(tVehicleInfo.getVehicleCode());
        tTerminalEquipmentInformationService.deleteTerminalEquipmentInformation(tVehicleInfo.getVehicleCode());
        tRelocationInfoService.deleteRelocationInfo(tVehicleInfo.getVehicleCode());
        tPlatformEquipmentInformationService.deletePlatformEquipmentInformation(tVehicleInfo.getVehicleCode());
        tOilConsumptionEquipmentService.deleteOilConsumptionEquipment(tVehicleInfo.getVehicleCode());
        tDriverInfoService.deleteDriverInfoInfo(tVehicleInfo.getVehicleCode());
        tEquipmentInfoService.deleteEquipmentInfo(tVehicleInfo.getEquipmentCode());
    }

    /**
     * 通过主键查询单条数据
     * @param id
     * @return
     */
    @Override
    public TVehicleInfoTotalResponseDto selectVehicleInfoById(Integer id) {
        TVehicleInfo tVehicleInfo = tVehicleInfoDao.selectById(id);
        TVehicleInfoTotalResponseDto tVehicleInfoTotalResponseDto=new TVehicleInfoTotalResponseDto();
        tVehicleInfoTotalResponseDto.setTVehicleOwnerInfoAddRequestDto(tVehicleOwnerInfoService.selectVehicleOwnerInfo(tVehicleInfo.getVehicleCode()));
        tVehicleInfoTotalResponseDto.setTVehicleDetailInfoAddRequestDto(tVehicleDetailInfoService.selectVehicleDetailInfo(tVehicleInfo.getVehicleCode()));
        tVehicleInfoTotalResponseDto.setTTerminalEquipmentInformationAddRequestDto(tTerminalEquipmentInformationService.selectTerminalEquipmentInformation(tVehicleInfo.getVehicleCode()));
        tVehicleInfoTotalResponseDto.setTRelocationInfoAddRequestDto(tRelocationInfoService.selectRelocationInfo(tVehicleInfo.getVehicleCode()));
        tVehicleInfoTotalResponseDto.setTPlatformEquipmentInformationAddRequestDto(tPlatformEquipmentInformationService.selectPlatformEquipmentInformation(tVehicleInfo.getVehicleCode()));
        tVehicleInfoTotalResponseDto.setTOilConsumptionEquipmentAddRequestDto(tOilConsumptionEquipmentService.selectOilConsumptionEquipment(tVehicleInfo.getVehicleCode()));
        tVehicleInfoTotalResponseDto.setTDriverInfoAddRequestDto(tDriverInfoService.selectDriverInfoInfo(tVehicleInfo.getVehicleCode()));
        tVehicleInfoTotalResponseDto.setTEquipmentInfoAddRequestDto(tEquipmentInfoService.selectEquipmentInfo(tVehicleInfo.getVehicleCode()));
        return tVehicleInfoTotalResponseDto;
    }
}
